﻿using DAL;
using Function;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;

namespace BLL
{
    public class JsonBLL
    {
        JsonDAL dal = new JsonDAL();
        /// <summary>
        /// 根据门店ID获取该门店同比和环比销售额
        /// </summary>
        /// <param name="shopid">门店ID</param>
        /// <returns></returns>
        public string GetOrderMoney(string shopid)
        {
            string now = "[", tb = "[", hb = "[";
            string json = "";
            DataSet ds = dal.GetOrderMoney(shopid);
            foreach (DataRow dr in ds.Tables[0].Rows)
            {
                if (now == "[")
                {
                    now += dr["allmoney"].ToString();
                }
                else
                {
                    now += "," + dr["allmoney"].ToString();
                }
                if (tb == "[")
                {
                    tb += dr["tbmoney"].ToString();
                }
                else
                {
                    tb += "," + dr["tbmoney"].ToString();
                }
                if (hb == "[")
                {
                    hb += dr["hbmoney"].ToString();
                }
                else
                {
                    hb += "," + dr["hbmoney"].ToString();
                }
            }
            now += "]";
            tb += "]";
            hb += "]";
            json = "[{name: '月销量额',data:" + now + "},{name: '月销量额同比',data:" + tb + "},{name: '月销量额环比',data:" + hb + "}]";
            if (ds.Tables[0].Rows != null && ds.Tables[0].Rows.Count > 0)
            {
                return json;
            }
            else
            {
                return "";
            }
        }
        /// <summary>
        /// 根据门店ID获取该门店同比和环比
        /// </summary>
        /// <param name="shopid">门店ID</param>
        /// <returns></returns>
        public string GetNum(string shopid)
        {
            string now = "[", tb = "[", hb = "[";
            string json = "";
            DataSet ds = dal.GetNum(shopid);
            foreach (DataRow dr in ds.Tables[0].Rows)
            {
                if (now == "[")
                {
                    now += dr["allNum"].ToString();
                }
                else
                {
                    now += "," + dr["allNum"].ToString();
                }
                if (tb == "[")
                {
                    tb += dr["tbNum"].ToString();
                }
                else
                {
                    tb += "," + dr["tbNum"].ToString();
                }
                if (hb == "[")
                {
                    hb += dr["hbNum"].ToString();
                }
                else
                {
                    hb += "," + dr["hbNum"].ToString();
                }
            }
            now += "]";
            tb += "]";
            hb += "]";
            json = "[{name: '月订单量',data:" + now + "},{name: '月订单量同比',data:" + tb + "},{name: '月订单量环比',data:" + hb + "}]";
            if (ds.Tables[0].Rows != null && ds.Tables[0].Rows.Count > 0)
            {
                return json;
            }
            else
            {
                return "";
            }
        }
        /// <summary>
        /// 根据门店ID和时间查询销售占比情况
        /// </summary>
        /// <param name="shopid">门店ID</param>
        /// <param name="date">时间</param>
        /// <returns></returns>
        public string GetOrderZb(string shopid, string date)
        {
            // [
            //    ['Firefox',   45.0],
            //    ['IE',       26.8],
            //    {
            //        name: 'Chrome',
            //        y: 12.8,
            //        sliced: true,
            //        selected: true
            //    },
            //    ['Safari',    8.5],
            //    ['Opera',     6.2],
            //    ['Others',   0.7]
            //]
            string json = "[";
            DataSet ds = dal.GetOrderZb(shopid, date);
            foreach (DataRow dr in ds.Tables[0].Rows)
            {
                json += "{name:'白酒',y:" + dr["Liquor"].ToString() + ",sliced: true,selected: true}";
                json += ",['红酒'," + dr["Wine"].ToString() + "]";
                json += ",['洋酒'," + dr["Foregn"].ToString() + "]";
                json += ",['啤酒'," + dr["Beer"].ToString() + "]";
                json += ",['黄酒、果酒、保健酒'," + dr["Care"].ToString() + "]";
                json += ",['礼品、赠品'," + dr["Gift"].ToString() + "]";
                json += ",['其他商品'," + dr["Other"].ToString() + "]";
            }
            json += "]";

            if (json == "[]")
            {
                return "";
            }
            return json;
        }
        /// <summary>
        /// 统计查询
        /// </summary>
        /// <param name="_pageCount">数据条数</param>
        /// <param name="dateType">查询类型  1为每天 2为每月 3为每年</param>
        /// <param name="shopid">门店ID</param>
        /// <param name="pagesize">页长</param>
        /// <param name="pageindex">当前页</param>
        /// <param name="_connection">连接字符串</param>
        /// <param name="_strMess">返回消息</param>
        /// <returns></returns>
        public static DataTable SelectPage(out int _pageCount, int dateType, int shopid, int pagesize, int pageindex, PubConstant.DataBaseTarget target, out string _strMess)
        {
            return JsonDAL.SelectPage(out _pageCount, dateType, shopid, pagesize, pageindex,  PubConstant.GetConnectString(target), out _strMess);
        }
    }
}
